package com.info33.platform.business.pay.controller;

import com.info33.platform.business.pay.dto.TextDTO;
import com.info33.platform.business.pay.entity.Text;
import com.info33.platform.business.pay.service.TextService;
import com.info33.platform.common.mvc.controller.BaseController;
import com.info33.platform.common.mvc.vo.Result;
import com.info33.platform.common.pay.WxPayV3Api;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("/payFunction")
@Api(tags = "支付功能")
public class TextController extends BaseController<TextService, Text> {

    private final TextService textService;

    @Autowired
    public TextController(TextService textService) {
        super(textService);
        this.textService = textService;
    }


    @PutMapping(value = "/app/pay")
    @ApiOperation(value = "支付按钮")
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public Result<Object> appPay(HttpServletRequest request, @RequestBody TextDTO dto) throws Exception {
        return Result.ok(textService.appPay(request, dto));
    }


    @RequestMapping("/payBack/zfbPay")
    @ApiOperation(value = "支付宝-支付回调接口")
    public void breakInfo(HttpServletRequest request, HttpServletResponse response) throws Exception {
        textService.breakInfo(request, response);
    }

    @PostMapping("/payBack/wxPay")
    @ResponseBody
    @ApiOperation(value = "微信-支付回调接口")
    public void payNotify(HttpServletRequest request, HttpServletResponse response) throws Exception {
        textService.wxPayNotify(request, response);
    }

    @PostMapping("/pay/zfbRefund")
    @ApiOperation(value = "支付宝-退款", notes = "传入订单id,商户订单号orderNumber,退款金额refundPrice")
    public Result<?> payZfbRefund(@RequestBody TextDTO dto) throws Exception {
        return textService.payZfbRefund(dto);
    }

    @PostMapping("/pay/wxRefund")
    @ApiOperation(value = "微信-退款", notes = "传入订单id,商户订单号orderNumber或者微信订单号payNo,实收款realPrice,退款金额refundPrice")
    public Result<?> payWxRefund(@RequestBody TextDTO dto) throws Exception {
        return textService.payWxRefund(dto);
    }

    @RequestMapping(value = "/payBack/wxRefund")
    @ApiOperation(value = "微信-退款回调接口")
    public void payWxRefund(HttpServletRequest request) {
        WxPayV3Api.refundNotify(request);
    }

    @RequestMapping("/get")
    @ResponseBody
    @ApiOperation(value = "微信-获取平台证书列表")
    public Result<String> v3Get() {
        return Result.ok(WxPayV3Api.v3Get());
    }
}
